package cn.online.qa.ctrl;

import cn.online.qa.entity.AjaxResult;
import cn.online.qa.entity.Page;
import cn.online.qa.entity.Questions;
import cn.online.qa.service.QuestionsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
public class QuestionsController {

    @Resource
    QuestionsService questionsService;

    //分页
     @RequestMapping(value = "quesions-list",produces = "application/json;charset=utf-8")
    public Page<Questions> questionslist(@RequestParam(value = "pagNo",defaultValue = "1") int pageNo, @RequestParam(value = "limit",defaultValue = "10") int limit){
      pageNo--;
        org.springframework.data.domain.Page<Questions> p= questionsService.paging(pageNo,limit);
         return Page.toPage(p);
     }
    //获得该问题
    @RequestMapping(value = "getQuestion",produces = "application/json;charset=utf-8")
    public AjaxResult getQuestion(@RequestParam("id") long id){
       Questions questions= questionsService.getQuestions(id);
       if(questions!=null){
           return AjaxResult.success("true",questions);
       }
       return  AjaxResult.error("false");
    }

    //添加问题
    @RequestMapping(value = "addquestions",produces = "application/json;charset=utf-8")
    public AjaxResult addQuestions(@RequestParam("title")String title, @RequestParam("detailDesc")String detailDesc){
        Questions questions=new Questions();
        questions.setTitle(title);
//         questions.setDetailDesc(detailDesc);
//         questions.setLastModified(new Date());
                 questions.setDetaildesc(detailDesc);
         questions.setLastmodified(new Date());
         int a= questionsService.addQuestions(questions);
         return a>0? AjaxResult.success("true"): AjaxResult.error("false");
    }

}
